<?php

class PromotionController extends GxController {

    public function filters() {
        return array('rights');
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Promotion'),
        ));
    }

    public function actionCreate() {
        $model = new Promotion;
        if (isset($_POST['Promotion'])) {
            $model->setAttributes($_POST['Promotion']);
            if ($model->save()) {
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'Promotion');
        if (isset($_POST['Promotion'])) {
            $model->setAttributes($_POST['Promotion']);       
            if ($model->save()) {
                $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Promotion')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

 

    public function actionAdmin() {
        $model = new Promotion('search');
        $model->unsetAttributes();

        if (isset($_GET['Promotion']))
            $model->setAttributes($_GET['Promotion']);
        $statuschangeurl = Yii::app()->createUrl('Promotion/statuschange/');
        $this->render('admin', array(
            'model' => $model, 'statuschangeurl' => $statuschangeurl,
        ));
    }

    public function actionStatuschange() {
        $model = $this->loadModel($_POST['id'], 'Promotion');
        $model->status = $_POST['status'];
        $model->save();
    }

}